package org.mnj07.web.servlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class HelloWorldServlet
 */

@WebServlet(
  name = "HelloWorldServlet",
  urlPatterns = {"/hello.html"},
  initParams = {
      @WebInitParam(name="message", value="Hello Universe!")
  }
)
public class HelloWorldServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	private String message;
	
	public void init(ServletConfig config) throws ServletException {
		super.init(config);
		this.message = config.getInitParameter("message");
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println("<html>");
        out.println("<head>");
        out.println("<title>" + this.message + "</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("<h1>" + this.message + "</h1>");
        out.println("</body>");
        out.println("</html>");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
